package v1

import (
	bc "application/blockchain"
	"application/pkg/app"
	"application/pkg/conn"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type User struct {
	Id       string `json:"id"`
	Username string `json:"username"`
	Password string `json:"password"`
	Email    string `json:"email"`
	Phone    string `json:"phone"`
	Faceurl  string `json:"faceurl"`
	Sex      string `json:"sex"`
}

func AddUser(c *gin.Context) {
	db := conn.GetDB()
	appG := app.Gin{C: c}
	body := new(User)
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	result := db.Create(&body)
	if result.RowsAffected == 1 {
		var user User
		db.Where(&User{Username: body.Username}, "username").Find(&user)
		appG.Response(http.StatusOK, "成功", fmt.Sprintf(user.Id))
	} else {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("数据库添加出错（可能是已有同名用户TT"))
	}
}

func VerifyUser(c *gin.Context) {
	db := conn.GetDB()
	appG := app.Gin{C: c}
	body := new(User)
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	var user User
	db.Where(&User{Username: body.Username}, "username").Find(&user)
	if user.Password == body.Password {
		var users []User
		res := db.Find(&users)
		if res.RowsAffected == 0 {
			appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("无法从数据库获取用户"))
			return
		}
		var userBytes [][]byte
		for _, val := range users {
			userBytes = append(userBytes, []byte(val.Id))
			userBytes = append(userBytes, []byte(val.Username))
			userBytes = append(userBytes, []byte(val.Password))
			userBytes = append(userBytes, []byte(val.Email))
			userBytes = append(userBytes, []byte(val.Phone))
			userBytes = append(userBytes, []byte(val.Sex))
		}
		//调用智能合约
		_, err := bc.ChannelExecute("updAccountList", userBytes)
		if err != nil {
			appG.Response(http.StatusInternalServerError, "失败", err.Error())
			return
		}
		fmt.Println("----------------------update AccountList successfully----------------------")
		appG.Response(http.StatusOK, "成功", user.Id)
	} else {
		res := "数据库验证出错" + "密码应该是：" + user.Password + "但是发现：" + body.Password + "-----此条仅用于调试"
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf(res))
	}
}

func GetUser(c *gin.Context) {
	db := conn.GetDB()
	appG := app.Gin{C: c}
	body := new(User)
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", fmt.Sprintf(err.Error())))
		return
	}
	res := db.Where(&User{Username: body.Username}, "username").Find(&body)
	if res.RowsAffected == 1 {
		appG.Response(http.StatusOK, "成功", fmt.Sprintf(body.Id))
	} else {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("数据库获取出错"))
	}
}

func UpdateUser(c *gin.Context) {
	db := conn.GetDB()
	appG := app.Gin{C: c}
	body := new(User)
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	res := db.Save(&body)
	if res.RowsAffected == 1 {
		appG.Response(http.StatusOK, "成功", fmt.Sprintf(body.Id))
	} else {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("数据库获取出错"))
	}
}

func ModifyPassword(c *gin.Context) {
	db := conn.GetDB()
	appG := app.Gin{C: c}
	body := new(User)
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	res := db.Model(&body).Update("password", body.Password)
	if res.RowsAffected == 1 {
		appG.Response(http.StatusOK, "成功", fmt.Sprintf(body.Id))
	} else {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("数据库获取出错"))
	}
}
